package cn.lotusnut.rabbitmqboot.configuration;

import com.rabbitmq.client.BuiltinExchangeType;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author JiangMing Zhou
 * @Date 2022/12/1 21:56
 * @Version 1.8
 * 基于rabbitmq延迟插件的延迟队列配置
 */
@Configuration
public class DelayConfiguration {

    @Bean
    public Queue delayQueue(){
        return new Queue("delay_queue");
    }

    @Bean
    public CustomExchange customExchange(){
        //设置初始化参数
        Map<String,Object> initParam = new HashMap<>(2);
        initParam.put("x-delayed-type", "direct");
        //创建交换机
        //1.交换机名
        //2.消息类型（x-delayed-message表示全是延迟消息）
        //3.是否持久化
        //4.是否自动删除交换机
        //5.初始化参数
        CustomExchange exchange = new CustomExchange("delay_exchange", "x-delayed-message", true, false, initParam);
        return exchange;
    }

    @Bean
    public Binding bindingDelayQueueToCustomExchange(Queue delayQueue,CustomExchange customExchange){
        return BindingBuilder.bind(delayQueue).to(customExchange).with("delay").noargs();
    }
}
